<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{detailData&&detailData.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="detailData&&detailData.aut_name" :label="time()">
        <template #icon>
          <img :src="detailData&&detailData.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div >
            <van-button @click="isfollow(false)" v-if="detailData&&detailData.is_followed" type="info" size="mini">已关注</van-button>
            <van-button @click="isfollow(true)" v-else icon="plus" type="info" size="mini" plain>关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="detailData&&detailData.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button @click="fabulous(true)" icon="good-job" v-if="detailData&&(detailData.attitude===1)"  type="danger" size="small">已点赞</van-button>
        <van-button @click="fabulous(false)" icon="good-job-o" type="danger" v-else plain size="small">点赞</van-button>
      </div>
    </div>
    <!-- 评论区域 -->
    <CommentList @refreshComments='refreshComments' @changeLike='changeLike' :commentsNum='commentsNum' :commentVal='comments' ></CommentList>
  </div>
</template>

<script>
import CommentList from './components/CommentList.vue'
import { commentDisLiking, commentLiking, getComments, disfabulousArticle, fabulousArticle, disfollowAuthor, followAuthor, getArticleDetail } from '@/api'
import { timeAgo } from '@/utils/date'
export default {
  data () {
    return {
      detailData: null,
      title: '',
      comments: [],
      commentsNum: 0
    }
  },
  components: { CommentList },
  async created () {
    const result = await getArticleDetail(this.$route.query.id)
    this.detailData = result.data.data
    const result1 = await getComments(this.$route.query.id)
    this.comments = result1.data.data.results
    this.commentsNum = result1.data.data
  },
  methods: {
    // 刷新评论区数据
    async refreshComments () {
      const result1 = await getComments(this.$route.query.id)
      this.comments = result1.data.data.results
    },
    time () {
      return timeAgo(this.detailData && this.detailData.pubdate)
    },
    isfollow (res) {
      if (res === true) {
        followAuthor(this.detailData.aut_id)
        this.detailData.is_followed = true
      } else {
        disfollowAuthor(this.detailData.aut_id)
        this.detailData.is_followed = false
      }
    },
    fabulous (res) {
      if (res === true) {
        disfabulousArticle(this.detailData.art_id)
        this.detailData.attitude = 0
      } else {
        fabulousArticle(this.detailData.art_id)
        this.detailData.attitude = 1
      }
    },
    // 点赞和取消点赞
    changeLike (res1, res2) {
      this.comments.map(async a => {
        if (a.com_id === res2) {
          if (res1 === true) {
            await commentDisLiking(res2)
            a.is_liking = false
            return a
          } else {
            await commentLiking(res2)

            a.is_liking = true
            return a
          }
        } else {
          return a
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
width: 100%;}
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
